package com.kitmobile.view;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kitmobile.R;
import com.kitmobile.data.KITDatabase;
import com.kitmobile.model.KITContact;
import com.kitmobile.model.KITContact.Frequency;

public class KITReminder extends Activity {
	private Button butSaveReminder;
	private CheckBox chkReocurring;
	private RadioButton rdWeekly, rdMonthly, rdYearly;
	private AutoCompleteTextView txtContactID;

	public static final int REQUEST_CHOOSE_CONTACT = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder);
        
        initViews();
    }
    private void initViews() {
    	txtContactID = (AutoCompleteTextView) findViewById(R.id.txtContactID);
    	chkReocurring = (CheckBox) findViewById(R.id.chkReoccuring);
    	rdWeekly = (RadioButton) findViewById(R.id.rdWeekly);
    	rdMonthly = (RadioButton) findViewById(R.id.rdMonthly);
    	rdYearly = (RadioButton) findViewById(R.id.rdYearly);
    	butSaveReminder = (Button) findViewById(R.id.butSave);   
    }
    
    public void saveReminder(View v) {
    	KITContact newReminder = new KITContact();
    	newReminder.setContactID(txtContactID.getText().toString());
    	
    	Frequency chkFrequency = Frequency.none;
    	Calendar reminderDate = new GregorianCalendar();
    	
    	if (rdWeekly.isChecked()) {
    		reminderDate.add(Calendar.WEEK_OF_YEAR,1);
    		if (chkReocurring.isChecked()) {
    			chkFrequency = Frequency.daily;
    		}
    	} else if(rdMonthly.isChecked()) {
    		reminderDate.add(Calendar.MONTH,1);
    		if (chkReocurring.isChecked()) {
    			chkFrequency = Frequency.monthly;
    		}    		
    	} else if (rdYearly.isChecked()) {
    		reminderDate.add(Calendar.YEAR,1);
    		if (chkReocurring.isChecked()) {
    			chkFrequency = Frequency.yearly;
    		}    		
    	}    	
    	
    	newReminder.setFrequency(chkFrequency);
    	
    	newReminder.setNextReminderDate(reminderDate.getTimeInMillis());
    	addReminder(newReminder);
    }
    
    private void chooseContact(View v) {
    	Intent chooseContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    	startActivityForResult(chooseContact, REQUEST_CHOOSE_CONTACT);
    }
    
    public void addReminder(KITContact contact) {
    	KITDatabase db = new KITDatabase(this);
    	db.open();
    	db.insert(contact);
    	db.close();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK) {
    		switch(requestCode) {
    		case REQUEST_CHOOSE_CONTACT:
    			String contactId = data.getData().getLastPathSegment();
    			Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI, null, Contacts._ID + "=?", new String[]{contactId}, null);
    			if (cursor.moveToFirst()) {
    				;
    			}
    			break;
    		}
    	}
    }
}
